
package com.shai.wool.channel.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.shai.wool.channel.entity.ChannelGrayVO;
import com.shai.wool.channel.param.ChannelGrayParam;
import com.shai.wool.channel.service.ChannelGrayService;
import com.shai.wool.common.controller.BaseController;
import com.shai.wool.common.param.SessionUserInfo;
import com.shai.wool.sensitiveword.SensitiveWordUtils;

/**
 * 渠道灰名单Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class ChannelGrayController extends BaseController {
    
    private Logger logger = LoggerFactory.getLogger(ChannelGrayController.class);
    
    @Autowired
    private ChannelGrayService channelGrayService;
    
    private final static String LIST_PAGE = "/channel/channel_gray_list_ajax";

    private static final String MY_LIST_PAGE = "/account/my_channel_gray";

    private static final String ADMIN_LIST_PAGE = "/admin/channel_gray_manage";

    @RequestMapping(value = "/check/channelgray/insert", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String insert(ChannelGrayVO channelGrayVO) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        if (SensitiveWordUtils.isContaintSensitiveWord(channelGrayVO.getGrayInfo(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "举报项中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(channelGrayVO.getGrayInfo(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }
        
        if (SensitiveWordUtils.isContaintSensitiveWord(channelGrayVO.getChannelName(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "渠道商信息中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(channelGrayVO.getChannelName(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }
        
        try {
            channelGrayVO.setChannelGrayId(UUID.randomUUID().toString());
            channelGrayVO.setPublishUser(seUser.getUserId());
            channelGrayVO.setDataStatus(0);
            channelGrayVO.setAuditStatus(1);
            Integer insertCount = channelGrayService.insert(channelGrayVO);
            if (insertCount != null && insertCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增渠道灰名单成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增渠道灰名单失败");
            }
        } catch (Exception e) {
            logger.error("新增渠道灰名单出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增渠道灰名单出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/uncheck/channelgray/list")
    public ModelAndView queryList(ChannelGrayParam channelGrayParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        channelGrayParam.setAuditStatus(2);
        List<ChannelGrayVO> dataList = channelGrayService.queryByPage(channelGrayParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", channelGrayParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/account/channelgray/list")
    public ModelAndView queryMyList(ChannelGrayParam channelGrayParam) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        channelGrayParam.setPublishUser(seUser.getUserId());
        
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<ChannelGrayVO> dataList = channelGrayService.queryByPage(channelGrayParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", channelGrayParam);
        return new ModelAndView(MY_LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/account/channelgray/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = channelGrayService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除渠道灰名单成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除渠道灰名单失败");
            }
        } catch (Exception e) {
            logger.error("删除渠道灰名单出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除渠道灰名单出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/admin/channelgray/list")
    public ModelAndView queryAdminList(ChannelGrayParam channelGrayParam) {
        if (channelGrayParam != null) {
            channelGrayParam.setAuditStatus(1);
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<ChannelGrayVO> dataList = channelGrayService.queryByPage(channelGrayParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", channelGrayParam);
        return new ModelAndView(ADMIN_LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/admin/channelgray/audit", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String audit(String channelGrayId, Integer auditStatus) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            ChannelGrayVO channelGrayVO = new ChannelGrayVO();
            channelGrayVO.setChannelGrayId(channelGrayId);
            channelGrayVO.setAuditStatus(auditStatus);
            channelGrayVO.setAuditUser(seUser.getUserId());
            channelGrayVO.setAuditTime(new Date(System.currentTimeMillis()));
            Integer updateCount = channelGrayService.update(channelGrayVO);
            if (updateCount != null && updateCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "审核渠道灰名单成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "审核渠道灰名单失败");
            }
        } catch (Exception e) {
            logger.error("审核渠道灰名单出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "审核渠道灰名单出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
}
